<template>
  <div>
    <el-dialog :title="form.id?'编辑部门':'新增部门'" :visible="showDialog" width="50%" @close="close">
      <el-form ref="fm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('close-dialog',false)">取 消</el-button>
        <el-button type="primary" @click="addDept">{{ form.id?'修改':'新增' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees.js'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentDept: {
      // 父部门数据 => 复杂类型数据，要通过函数返回默认值
      type: Object,
      default: () => ({})
      //   default: () => {
      //     return {}
      //   }
    },
    // 所有部门数据
    allDepts: {
      type: Array,
      default: () => ({})
    }
  },
  data () {
    const validCode = (rule, value, cb) => {
      let flag
      if (this.form.id) {
        flag = this.allDepts.some(item => { return item.code === value && value !== this.form.code })
      } else {
        flag = this.allDepts.some(item => { item.code === value })
      }
      if (flag) {
        cb(new Error('部门编码重复了'))
      }
      cb()
    }
    return {
      peoples: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validate: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getPeoples()
  },
  methods: {
    async getPeoples () {
      const peoples = await getEmployeeSimple()
      // console.log(peoples)
      this.peoples = peoples
    },
    close () {
      // console.log('弹层关闭了')
      // 通过子传父修改showDialog关闭弹层
      this.$emit('close-dialog', false)
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清除校验残留
      this.$refs.fm.resetFields()
    },
    // 新增部门
    async addDept () {
      try {
        await this.$refs.fm.validate()
        // 校验通过
        // console.log('tongguo')
        if (this.form.id) {
          await updateDepartments(this.form)
        } else {
          await addDepartments({ ...this.form, pid: this.parentDept.id || '' })
        }

        this.$emit('close-dialog', false)
        this.$emit('refresh')
        this.$message.success('操作成功')
      } catch (error) {
        // 校验失败
        return error
      }
    },
    // 获取编辑部门详情数据 =》回填
    async getDeptDetail (id) {
      const res = await getDepartDetail(id)
      console.log(res)
      this.form = res
    }
  }
}

</script>

<style lang="scss" scoped>
</style>
